<template>
  <div class="container">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      error-text="获取用户文章失败"
      :error="error"
    >
      <article-item
        :photo="photo"
        v-for="(item,index) in list"
        :key="index"
        :data="item"
      ></article-item>
    </van-list>
  </div>
</template>
<script>
import { getUserAllArticle } from '@/api/article'
import articleItem from './article-item'
export default {
  components: {
    articleItem
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      pageSize: 10,
      pageNum: 1,
      error: false
    }
  },
  props: {
    userId: {
      type: [String, Number, Object],
      required: true
    },
    photo: {
      type: [String, Number, Object]
    }
  },
  methods: {
    async  onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      try {
        const { data: res } = await getUserAllArticle({
          id: this.userId,
          page: this.pageNum,
          per_page: this.pageSize
        })
        this.list.push(...res.data.results)
        this.pageNum++
        if (res.data.results.length === 0) {
          this.finished = true
        }
      } catch (err) {
        this.error = true
      }
      // 加载状态结束
      this.loading = false
      // 数据全部加载完成
    }
  }
}
</script>
